package org.qee.pubsub;

import org.qee.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisPubSubService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public long publish(String topic, Object content) {
        String obj2String = JsonUtil.obj2String(content);
        return redisTemplate.getConnectionFactory().getConnection().publish(topic.getBytes(), obj2String.getBytes());
    }

    public void subcribe(String topic, MessageListener messageListener) {
        redisTemplate.getConnectionFactory().getConnection().subscribe(messageListener, topic.getBytes());
    }
}
